package com.example.testone.uiComponents;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.testone.R;

public class BorderLayout extends FrameLayout {

    //边界线宽度
    private int mBorderWidthSize;
    //边界线颜色
    private int mBorderColor;

    //虚线宽度
    private float mDashWidth = -1;
    //虚线间隔
    private float mDashGap = -1;

    //圆角角度
    private float mLeftTopRadius;

    private float mRightTopRadius;

    private float mRightBottomRadius;

    private float mLeftBottomRadius;

    private float mRadius;

    @ColorInt
    private int mBackgroundColor = Color.TRANSPARENT;

    public BorderLayout(@NonNull Context context) {
        super(context);
        init(null);
    }

    public BorderLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        if (null != attrs) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.BorderLayout);
            mBorderWidthSize = typedArray.getDimensionPixelSize(R.styleable.BorderLayout_borderWidth, 0);
            mBorderColor = typedArray.getColor(R.styleable.BorderLayout_borderColor, Color.TRANSPARENT);
            mDashWidth = typedArray.getDimensionPixelSize(R.styleable.BorderLayout_borderDashWidth, -1);
            mDashGap = typedArray.getDimensionPixelSize(R.styleable.BorderLayout_borderDashGap, -1);
            mLeftTopRadius = typedArray.getDimensionPixelSize(R.styleable.BorderLayout_borderLeftTopRadius, 0);
            mRightTopRadius = typedArray.getDimensionPixelSize(R.styleable.BorderLayout_borderRightTopRadius, 0);
            mRightBottomRadius = typedArray.getDimensionPixelSize(R.styleable.BorderLayout_borderRightBottomRadius, 0);
            mLeftBottomRadius = typedArray.getDimensionPixelSize(R.styleable.BorderLayout_borderLeftBottomRadius, 0);
            mRadius = typedArray.getDimensionPixelSize(R.styleable.BorderLayout_borderRadius, 0);
            mBackgroundColor = typedArray.getColor(R.styleable.BorderLayout_backgroundColor, Color.TRANSPARENT);
            typedArray.recycle();
        }
        setBackground(buildBackGround());
    }

    private Drawable buildBackGround() {

        BorderDrawableBuilder builder = new BorderLayout.BorderDrawableBuilder()
                .width(mBorderWidthSize)
                .color(mBorderColor)
                .dashWidth(mDashWidth)
                .dashGap(mDashGap)
                .radius(mRadius)
                .backgroundColor(mBackgroundColor);

        if (mLeftTopRadius > 0) {
            builder.leftTopRadius(mLeftTopRadius);
        }
        if (mRightTopRadius > 0) {
            builder.rightTopRadius(mRightTopRadius);
        }
        if (mRightBottomRadius > 0) {
            builder.rightBottomRadius(mRightBottomRadius);
        }
        if (mLeftBottomRadius > 0) {
            builder.leftBottomRadius(mLeftBottomRadius);
        }

        return builder.build();
    }

    public static class BorderDrawableBuilder{
        private int mWidth;

        @ColorInt
        private int mColor;

        private float mDashWidth = -1;

        private float mDashGap = -1;

        private float mLeftTopRadius;

        private float mRightTopRadius;

        private float mRightBottomRadius;

        private float mLeftBottomRadius;

        @ColorInt
        private int mBackgroundColor = Color.TRANSPARENT;

        public BorderDrawableBuilder width(int width) {
            mWidth = width;
            return this;
        }

        public BorderDrawableBuilder color(@ColorInt int color) {
            mColor = color;
            return this;
        }

        public BorderDrawableBuilder dashWidth(float dashWidth) {
            mDashWidth = dashWidth;
            return this;
        }

        public BorderDrawableBuilder dashGap(float dashGap) {
            mDashGap = dashGap;
            return this;
        }

        public BorderDrawableBuilder leftTopRadius(float radius) {
            mLeftTopRadius = radius;
            return this;
        }

        public BorderDrawableBuilder rightTopRadius(float radius) {
            mRightTopRadius = radius;
            return this;
        }

        public BorderDrawableBuilder rightBottomRadius(float radius) {
            mRightBottomRadius = radius;
            return this;
        }

        public BorderDrawableBuilder leftBottomRadius(float radius) {
            mLeftBottomRadius = radius;
            return this;
        }

        public BorderDrawableBuilder radius(float radius) {
            mLeftTopRadius = radius;
            mRightTopRadius = radius;
            mRightBottomRadius = radius;
            mLeftBottomRadius = radius;
            return this;
        }

        public BorderDrawableBuilder backgroundColor(@ColorInt int color) {
            mBackgroundColor = color;
            return this;
        }

        public Drawable build() {
            GradientDrawable gd = new GradientDrawable();
            if (mDashWidth == -1 || mDashGap == -1) {
                gd.setStroke(mWidth, mColor);
            } else {
                gd.setStroke(mWidth, mColor, mDashWidth, mDashGap);
            }
            gd.setCornerRadii(new float[]{mLeftTopRadius, mLeftTopRadius, mRightTopRadius, mRightTopRadius, mRightBottomRadius, mRightBottomRadius, mLeftBottomRadius, mLeftBottomRadius});
            gd.setColor(mBackgroundColor);
            return gd;
        }
    }
}
